package com.gxa.weixing.portal.controller;

import com.gxa.weixing.core.common.JsonResult;
import com.gxa.weixing.core.exception.BusinessException;
import com.gxa.weixing.core.pojo.*;
import com.gxa.weixing.portal.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author HHT
 * 2022/1/6
 */
@RestController
@RequestMapping("/goods")
@Api(value = "goodsController",tags = "商品的接口")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @GetMapping("/getAll")
    @ApiOperation("获取商品的接口")
    public Map getAllGoods(){
        List<Goods> all = goodsService.findAll();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        jsonResult.setData(all);
        return jsonResult.getDataMap();
    }

    @GetMapping("/selectById")
    @ApiOperation("获取商品详情的接口")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id",value = "商品id",required = true,dataType = "Long")
    )
    public Map selectById(@RequestParam Long id){
        GoodDetailVO goodDetailVO = goodsService.selectById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        jsonResult.setData(goodDetailVO);
        return jsonResult.getDataMap();
    }

    @PostMapping("/addCart")
    @ApiOperation("把商品加入购物车")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "map",value = "map对象",required = true,dataType = "Long"),

            })
    public Map addCart(@RequestBody Map map){
        Integer type = Integer.valueOf(map.get("type").toString());
        Long userId = Long.valueOf(map.get("userId").toString());
        Long id = Long.valueOf(map.get("id").toString());
        String name=map.get("name").toString();
        BigDecimal price = BigDecimal.valueOf(Double.parseDouble(map.get("price").toString()));
        BigDecimal prePrice = BigDecimal.valueOf(Double.parseDouble(map.get("prePrice").toString()));
        BigDecimal purchasePrice = BigDecimal.valueOf(Double.parseDouble(map.get("purchasePrice").toString()));
        String desc=map.get("desc").toString();
        String img=map.get("img").toString();
        Integer integer = Integer.valueOf(map.get("state").toString());
        Goods goods=new Goods(id,name,price,prePrice,purchasePrice,desc,img,integer,null);
        goodsService.addGoodToCart(goods,userId,type);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @GetMapping("/findAllFromCart")
    @ApiOperation("获取购物车的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "Long")
    })
    public Map findAllFromCart(Long id){
        List<GetGoodsFromCartVO> allFromCart = goodsService.findAllFromCart(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        jsonResult.setData(allFromCart);
        return jsonResult.getDataMap();
    }

    @PostMapping("/deleteGoodFromCart")
    @ApiOperation("删除购物车里面的商品")

    public Map deleteGoodFromCart(@RequestBody DeleteGoodFromCartVO deleteGoodFromCartVO){

        List<Long> longs = deleteGoodFromCartVO.getLongs();
        Long userId = deleteGoodFromCartVO.getUserId();
        System.out.println(deleteGoodFromCartVO);
        goodsService.deleteGoodFromCart(longs,userId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");

        return jsonResult.getDataMap();
    }

    @PostMapping("/returnGoods")
    @ApiOperation("退货")
    @ApiImplicitParams(
            { @ApiImplicitParam(name = "goodId",value = "商品id",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "userId",value = "用户id",required = true,dataType = "Long")}
    )
    public Map returnGood(Long goodId,Long userId){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @GetMapping("/exchangeGoodsList")
    @ApiOperation("兑换商品列表")
    public Map exchangeGoodsList(){
        List<ForGoods> forGoods = goodsService.exchangeGoodsList();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        jsonResult.setData(forGoods);
        return jsonResult.getDataMap();
    }

    @GetMapping("/exchangeGoodDetail")
    @ApiOperation("兑换商品详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "需要兑换商品的详情",required = true,dataType = "Long")
    })
    public Map exchangeGoodDetail(Long id){
        ForGoodsDetailVo forGoodsDetailVo = goodsService.selectByIdForGoods(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        jsonResult.setData(forGoodsDetailVo);
        return jsonResult.getDataMap();
    }

    @GetMapping("/exchangeGood")
    @ApiOperation("兑换商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户ID",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "id",value = "商品id",required = true,dataType = "Long")
    })
    public Map exchangeGood(Long userId,Long id) throws BusinessException {
        goodsService.byExchangeGoods(userId,id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

}
